package com.digitalchina.lxnsy.fragment;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.bumptech.glide.Glide;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.farmVideo.HobbiesSelectActivity;
import com.digitalchina.lxnsy.activity.farmVideo.VideoListActivity;
import com.digitalchina.lxnsy.activity.old.ActivityDetailActivity;
import com.digitalchina.lxnsy.activity.old.ActivityListActivity;
import com.digitalchina.lxnsy.activity.old.AgriculturalServiceDetailActivity;
import com.digitalchina.lxnsy.activity.old.AgriculturalServiceListActivity;
import com.digitalchina.lxnsy.activity.old.AgriculturalTrainingDetailsActivity;
import com.digitalchina.lxnsy.activity.old.AnswerListActivity;
import com.digitalchina.lxnsy.activity.old.ConsultDynamicDetailActivity;
import com.digitalchina.lxnsy.activity.old.ConsultDynamicListActivity;
import com.digitalchina.lxnsy.activity.old.ConsultIssueV2Activity;
import com.digitalchina.lxnsy.activity.old.ExpertConsultationActivity;
import com.digitalchina.lxnsy.activity.old.ExpertsDetailActivity;
import com.digitalchina.lxnsy.activity.old.FamousInterviewActivity;
import com.digitalchina.lxnsy.activity.old.FarmingInfoDetailActivity;
import com.digitalchina.lxnsy.activity.old.FarmingInformationActivity;
import com.digitalchina.lxnsy.activity.old.IntegralLotteryActivity;
import com.digitalchina.lxnsy.activity.old.InterviewDetailActivity;
import com.digitalchina.lxnsy.activity.old.PersonnelServiceListActivity;
import com.digitalchina.lxnsy.activity.old.ProductDetailActivity;
import com.digitalchina.lxnsy.activity.old.ProductListActivity;
import com.digitalchina.lxnsy.activity.old.RealTimePriceTabActivity;
import com.digitalchina.lxnsy.activity.old.SelectProductionActivityV2;
import com.digitalchina.lxnsy.activity.old.ShareFarmDetailActivity;
import com.digitalchina.lxnsy.activity.old.ShareFarmListActivity;
import com.digitalchina.lxnsy.activity.old.StandardProductionActivity;
import com.digitalchina.lxnsy.activity.old.TalentsDetailsActivity;
import com.digitalchina.lxnsy.activity.old.TechnologiesDetailActivity;
import com.digitalchina.lxnsy.activity.old.TechnologiesListActivity;
import com.digitalchina.lxnsy.activity.old.TechnologyListActivity;
import com.digitalchina.lxnsy.activity.old.TechnologyTrainingVideoListActivityV2;
import com.digitalchina.lxnsy.activity.old.TrainInfoListActivity;
import com.digitalchina.lxnsy.activity.old.WebViewBannerShareActivity;
import com.digitalchina.lxnsy.adapter.HomePageRecyclerAdapter;
import com.digitalchina.lxnsy.api.BusinessActivity;
import com.digitalchina.lxnsy.api.BusinessConsultDynamic;
import com.digitalchina.lxnsy.api.BusinessInformation;
import com.digitalchina.lxnsy.api.BusinessShareFarm;
import com.digitalchina.lxnsy.api.BusinessTechnology;
import com.digitalchina.lxnsy.base.BaseV4Fragment;
import com.digitalchina.lxnsy.constant.Constant;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.ActivityListVO;
import com.digitalchina.lxnsy.entity.BannerVO;
import com.digitalchina.lxnsy.entity.ConsultDynamicListVO;
import com.digitalchina.lxnsy.entity.ConsultDynamicTypeVO;
import com.digitalchina.lxnsy.entity.FarmingInfoListVO;
import com.digitalchina.lxnsy.entity.TrainVO;
import com.digitalchina.lxnsy.interfaces.IRecyclerType;
import com.digitalchina.lxnsy.sy.all.MsgActivity;
import com.digitalchina.lxnsy.sy.login.LoginActivity;
import com.digitalchina.lxnsy.sy.peopleAddExp.ExpertQuestActivity;
import com.digitalchina.lxnsy.sy.peopleAddExp.QuickQuestionActivity;
import com.digitalchina.lxnsy.sy.peopleAddExp.RealTimePriceActivity;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.TimeUtil;
import com.digitalchina.lxnsy.xml.UserXML;
import com.facebook.drawee.view.SimpleDraweeView;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bingoogolapple.bgabanner.BGABanner;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：APP首页
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/3/24 11:53:56
 * 修改备注：
 * ================================================
 */

public class Main1Fragment extends BaseV4Fragment implements View.OnClickListener {


    private LinearLayout rlExpertConsultation; // 专家咨询
    private LinearLayout rlExpertFastAsk; // 快速提问
    private View headView;//头布局
    private List<IRecyclerType> allList = new ArrayList<>();
    private List<ConsultDynamicListVO> consultDynamicListVOList = new ArrayList<>();
    private List<ActivityListVO> activityVOList = new ArrayList<>();
    private List<FarmingInfoListVO> shareVOList = new ArrayList<>();

    private int msgWhat;

    //LBS
    private final int SDK_PERMISSION_REQUEST = 127;


    private LinearLayout tvJsdq, tvZthd, tvGxsn;

    private SimpleDraweeView sdv_images, sdv_images2, sdv_images3,sdv_images11,sdv_images12;
    private TextView tv_title, tv_title2, tv_title3;
    private TextView tv_price, tv_price2, tv_price3;
    private TextView tv_type, tv_type2, tv_type3;
    private TextView tv_tel, tv_time, tv_issue;


    private LinearLayout rlVideo;
    private LinearLayout rlPrice;
    private LinearLayout rlTalents, rdzx_linear, zxdt_linear, rmhd_linear;
 private RelativeLayout allLinear_1,allLinear_2,allLinear_3,allLinear_4,allLinear_5,allLinear_6;
  private LinearLayout  msg_linear;
    @Override
    public View setRootView() {


        headView = inflate(R.layout.view_main_v1);
        return headView;
    }

    @Override
    public void initMember() {

        hideActionBar();

        rlExpertConsultation = headView.findViewById(R.id.headview_home_page_tv_expert_consultation);
        rlExpertFastAsk = headView.findViewById(R.id.headview_home_page_tv_fast_ask);
        rlVideo = headView.findViewById(R.id.rl_video);
        rlPrice = headView.findViewById(R.id.rl_price);
        rlTalents = headView.findViewById(R.id.rl_talents);
        //新增四个模块
        tvJsdq = headView.findViewById(R.id.tv_jsdq);
        tvZthd = headView.findViewById(R.id.tv_zthd);
        tvGxsn = headView.findViewById(R.id.tv_gxsn);
        rdzx_linear = headView.findViewById(R.id.rdzx_linear);
        zxdt_linear = headView.findViewById(R.id.zxdt_linear);
        rmhd_linear = headView.findViewById(R.id.rmhd_linear);

        sdv_images = headView.findViewById(R.id.sdv_images);
        sdv_images2 = headView.findViewById(R.id.sdv_images2);
        sdv_images3 = headView.findViewById(R.id.sdv_images3);
        sdv_images11 = headView.findViewById(R.id.sdv_images11);

        sdv_images12 = headView.findViewById(R.id.sdv_images12);


        tv_title = headView.findViewById(R.id.tv_title);
        tv_title2 = headView.findViewById(R.id.tv_title2);
        tv_title3 = headView.findViewById(R.id.tv_title3);

        tv_price = headView.findViewById(R.id.tv_price);
        tv_price2 = headView.findViewById(R.id.tv_price2);
        tv_price3 = headView.findViewById(R.id.tv_price3);

        tv_type = headView.findViewById(R.id.tv_type);
        tv_type2 = headView.findViewById(R.id.tv_type2);
        tv_type3 = headView.findViewById(R.id.tv_type3);

        tv_tel = headView.findViewById(R.id.tv_tel);
        tv_time = headView.findViewById(R.id.tv_time);
        tv_issue = headView.findViewById(R.id.tv_issue);
        allLinear_1= headView.findViewById(R.id.allLinear_1);
        allLinear_2= headView.findViewById(R.id.allLinear_2);
        allLinear_3= headView.findViewById(R.id.allLinear_3);
        allLinear_4= headView.findViewById(R.id.allLinear_4);
        allLinear_5= headView.findViewById(R.id.allLinear_5);
        allLinear_6= headView.findViewById(R.id.allLinear_6);
        msg_linear= headView.findViewById(R.id.msg_linear);
        getPersimmions();

        onRefresh();
    }


    @TargetApi(23)
    private void getPersimmions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ArrayList<String> permissions = new ArrayList<>();
            /***
             * 定位权限为必须权限，用户如果禁止，则每次进入都会申请
             */
            // 定位精确位置
            if (getActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
            }
            if (getActivity().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
            }

            if (permissions.size() > 0) {
                requestPermissions(permissions.toArray(new String[permissions.size()]), SDK_PERMISSION_REQUEST);
            }
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @TargetApi(23)
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);


    }

    /**
     * 获取热门回答列表(农技智库列表)
     */
    public void getTechnology() {
        BusinessTechnology.getTechnologyList(getActivity(), "", String.valueOf(1), String.valueOf(3), mHandler);
    }

    @Override
    public void addListener() {

        rlExpertConsultation.setOnClickListener(this);//专家咨询
        rlExpertFastAsk.setOnClickListener(this);//快速提问

        rlVideo.setOnClickListener(this);
        rlPrice.setOnClickListener(this);
        rlTalents.setOnClickListener(this);


        tvJsdq.setOnClickListener(this);
        tvZthd.setOnClickListener(this);
        tvGxsn.setOnClickListener(this);
        rdzx_linear.setOnClickListener(this);
        zxdt_linear.setOnClickListener(this);
        rmhd_linear.setOnClickListener(this);
        allLinear_1.setOnClickListener(this);
        allLinear_2.setOnClickListener(this);
        allLinear_3.setOnClickListener(this);
        allLinear_4.setOnClickListener(this);
        allLinear_5.setOnClickListener(this);
        allLinear_6.setOnClickListener(this);
        msg_linear.setOnClickListener(this);
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                msgWhat = msg.what;

                LogUtils.e("请求结果" + msg.what);
                switch (msg.what) {
                    case MSG.MSG_GET_TRAINING_IN_CITY_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_TECHNOLOGY_LIST_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_TECHNOLOGIES_SUCCESS: //技术大全
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_QUERY_CONSULT_LIST_SUCCESS: //咨询动态
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_SEARCH_ACTIVITY_SUCCESS: // 热门活动
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_INFOMATION_LIST_BY_TYPE_SUCCESS: // 共享三农
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_PLAN_COUNT_BY_USER_SUCCESS: // 获取种植标准数量
                        String count = FastJsonUtil.getString(msg.obj.toString(), "count");
                        if (!StringUtil.isStrEmpty(count)) {
                            if (Integer.parseInt(count) <= 0)
                                GotoUtil.gotoActivity(getActivity(), SelectProductionActivityV2.class);
                            else
                                GotoUtil.gotoActivity(getActivity(), StandardProductionActivity.class);
                        } else
                            showToast("获取数据失败");
                        break;
                    case MSG.MSG_GET_PLAN_COUNT_BY_USER_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_CHECK_USER_PERMISSON_SUCCESS: // 是否有企业权限
                        String s = FastJsonUtil.getString(msg.obj.toString());
                        break;
                    case MSG_S_0: // 是否选过类型
                        String isSelect = FastJsonUtil.getString(msg.obj.toString(), "isSelect");
                        if ("1".equals(isSelect)) {
                            UserXML.setVal(getActivity(), "isSelectType", "1");
                            GotoUtil.gotoActivity(getActivity(), VideoListActivity.class);
                        } else {
                            GotoUtil.gotoActivity(getActivity(), HobbiesSelectActivity.class);
                        }
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object)  {

        switch (msgWhat) {

            case MSG.MSG_QUERY_CONSULT_LIST_SUCCESS://咨询动态

                consultDynamicListVOList = FastJsonUtil.getListBeanRow(object.toString(), "rows", ConsultDynamicListVO.class);

                tv_tel.setText(consultDynamicListVOList.get(0).getUserInfo().mobilePhone.replaceAll("(\\d{3})\\d{7}(\\d{1})", "$1*******$2"));
                try {
                    tv_time.setText(consultDynamicListVOList.get(0).getCreateTime() != null? TimeUtil.getDatebefore2(consultDynamicListVOList.get(0).getCreateTime()):"");
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                tv_issue.setText(consultDynamicListVOList.get(0).getContent());

                break;
            case MSG.MSG_SEARCH_ACTIVITY_SUCCESS:// 热门活动

                activityVOList = FastJsonUtil.getListBeanRow(object.toString(), "rows", ActivityListVO.class);

                String[] arr= activityVOList.get(0).getImgPath().split(",");
                FrescoUtil.showImageSmall(arr[0], sdv_images11);
                String[] arr1= activityVOList.get(1).getImgPath().split(",");


                FrescoUtil.showImageSmall(arr1[0], sdv_images12);





                break;
            case MSG.MSG_GET_INFOMATION_LIST_BY_TYPE_SUCCESS://共享精选

                shareVOList = FastJsonUtil.getListBeanRow(object.toString(), "rows", FarmingInfoListVO.class);

                FrescoUtil.showImageSmall(shareVOList.get(0).getImgUrl(), sdv_images);
                FrescoUtil.showImageSmall(shareVOList.get(1).getImgUrl(), sdv_images2);
                FrescoUtil.showImageSmall(shareVOList.get(2).getImgUrl(), sdv_images3);
                tv_title.setText(shareVOList.get(0).getTitle());
                tv_title2.setText(shareVOList.get(1).getTitle());
                tv_title3.setText(shareVOList.get(2).getTitle());
                tv_price.setText(shareVOList.get(0).getCreateTime());
                tv_price2.setText(shareVOList.get(1).getCreateTime());
                tv_price3.setText(shareVOList.get(2).getCreateTime());
                tv_type.setText(shareVOList.get(0).getAuthor());
                tv_type2.setText(shareVOList.get(1).getAuthor());
                tv_type3.setText(shareVOList.get(2).getAuthor());


                break;
        }

    }


    @Override
    public void rightOnClick() {

    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.headview_home_page_tv_expert_consultation:
                //专家咨询
                noLoginToSkip(ExpertQuestActivity.class);
                break;
            case R.id.rl_video:
                //农科大讲堂
                GotoUtil.gotoActivity(getActivity(), TechnologyTrainingVideoListActivityV2.class);
                break;
            case R.id.rl_price:
                //实时价格
                GotoUtil.gotoActivity(getActivity(), RealTimePriceActivity.class);
                break;
            case R.id.tv_jsdq: // 技术大全
                GotoUtil.gotoActivity(getActivity(), TechnologiesListActivity.class);
                break;
            case R.id.tv_zthd: // 专题活动
                GotoUtil.gotoActivity(getActivity(), ActivityListActivity.class);
                break;
            case R.id.tv_gxsn: // 共享三农
                GotoUtil.gotoActivity(getActivity(), ShareFarmListActivity.class);
                break;
            case R.id.rl_talents:
                //人才服务-农活帮
                GotoUtil.gotoActivity(getActivity(), PersonnelServiceListActivity.class);
                break;
            case R.id.headview_home_page_tv_fast_ask:
                //快速提问
                noLoginToSkip(QuickQuestionActivity.class);
                break;

            case R.id.rdzx_linear:
                GotoUtil.gotoActivity(getActivity(), FarmingInformationActivity.class);
                break;
            case R.id.zxdt_linear:

                GotoUtil.gotoActivity(getActivity(), ConsultDynamicListActivity.class);

                break;
            case R.id.rmhd_linear:

                GotoUtil.gotoActivity(getActivity(), ActivityListActivity.class);

                break;
            case R.id.allLinear_1:
                GotoUtil.gotoActivity(getActivity(), FarmingInfoDetailActivity.class, "FarmingInfoListVO",shareVOList.get(0) );

                break;
            case R.id.allLinear_2:
                GotoUtil.gotoActivity(getActivity(), FarmingInfoDetailActivity.class, "FarmingInfoListVO",shareVOList.get(1) );

                break;
            case R.id.allLinear_3:
                GotoUtil.gotoActivity(getActivity(), FarmingInfoDetailActivity.class, "FarmingInfoListVO",shareVOList.get(2) );

                break;
            case R.id.allLinear_4:

//                if ("1".equals(UserXML.getLoginStatus(getActivity()))) {
                    GotoUtil.gotoActivity(getActivity(), ConsultDynamicDetailActivity.class, "consultDynamicListVO", consultDynamicListVOList.get(0));
//                } else {
//                    GotoUtil.gotoActivity(getActivity(), LoginActivity.class);
//                }

                break;
            case R.id.allLinear_5:
                GotoUtil.gotoActivity(getActivity(), ActivityDetailActivity.class, "ActivityVO",activityVOList.get(0) );

                break;
            case R.id.allLinear_6:
                GotoUtil.gotoActivity(getActivity(), ActivityDetailActivity.class, "ActivityVO",activityVOList.get(1) );

                break;
            case R.id.msg_linear:

                noLoginToSkip(MsgActivity.class);

                break;


        }
    }


    public void onRefresh() {
        //热点资讯====农业资讯
        BusinessInformation.getInformationListByType(getActivity(), "", 1, 3, "1", mHandler);

        // 咨询动态
        BusinessConsultDynamic.queryConsultList(getActivity(), 1, 1, "1", mHandler);
        //热门活动
        BusinessActivity.searchActivity(getActivity(), "", "", "", 1, 2, "1", mHandler);
    }


    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
